package com.shcz.migu.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

@SuppressLint("ShowToast")
public final class CommonToast {

	private static Toast mToast;
	private static int mDuration = Toast.LENGTH_SHORT;
	private static Context mContext;
	
	public static void init(Context context) {
		mContext = context;
		mToast = Toast.makeText(context, "", mDuration);
	}

	public static boolean isNotInit() {
		return mContext == null;
	}
	
	public static void showToast(String text){
		showToast(text, Toast.LENGTH_SHORT);
    }
	
	public static void showCenterToast(String text){
		showCenterToast(text, Toast.LENGTH_SHORT);
	}
    
    public static void showToast(int resId){
    	if(resId > 0){
	    	mToast.setText(resId);
			mToast.setDuration(Toast.LENGTH_SHORT);
			mToast.setGravity(Gravity.BOTTOM, 0, ConvertDipPx.dip2px(mContext, 60));
            if(mToast.getView() instanceof LinearLayout){
                if(((LinearLayout)mToast.getView()).getChildAt(0) instanceof ImageView){
                    ((LinearLayout)mToast.getView()).removeViewAt(0);
                }
            }

			mToast.show();
		}
    }
    

	public static void showToast(String text, int duration){
//		if (mContext == null) {
//			showNormalToast(text, duration);
//		return;
//		}
//		showTitleBarToast(text, duration);
		showNormalToast(text, duration);
	}

	private static void showTitleBarToast(String text, int duration) {
//		Alerter.create((Activity) mContext)
//				.setTitle("警告")
//				.setText(text)
//				.setBackgroundColorRes(R.color.orange_gray)
//				.show();
	}

	private static void showNormalToast(String text, int duration) {
		if (!TextUtils.isEmpty(text)) {
			mToast.setText(text);
			mToast.setDuration(duration);
			mToast.setGravity(Gravity.BOTTOM, 0, ConvertDipPx.dip2px(mContext, 60));
			if (mToast.getView() instanceof LinearLayout) {
				if (((LinearLayout) mToast.getView()).getChildAt(0) instanceof ImageView) {
					((LinearLayout) mToast.getView()).removeViewAt(0);
				}
			}
			mToast.show();
		}
		return;
	}

	public static void showCenterToast(String text, int duration){

		if(!TextUtils.isEmpty(text)){
	    	mToast.setText(text);
			mToast.setDuration(duration);
			mToast.setGravity(Gravity.CENTER, 0, 0);
            if(mToast.getView() instanceof LinearLayout){
                if(((LinearLayout)mToast.getView()).getChildAt(0) instanceof ImageView){
                    ((LinearLayout)mToast.getView()).removeViewAt(0);
                }
            }
			mToast.show();
		}
    }
	
	
	public static void showToast(Activity activity, String text, int iconResId, int type) {
		if(!TextUtils.isEmpty(text)){
	    	mToast.setText(text);
			mToast.setDuration(type);
			if(iconResId > 0){
				mToast.setGravity(Gravity.CENTER, 0, 0);
                if(mToast.getView() instanceof LinearLayout){
                    LinearLayout toastView = (LinearLayout) mToast.getView();
                    if(toastView.getChildAt(0) instanceof ImageView){
                        ((ImageView) toastView.getChildAt(0)).setImageResource(iconResId);
                    } else {
                        ImageView toastImageView = new ImageView(activity);
                        toastImageView.setImageResource(iconResId);
                        toastView.addView(toastImageView, 0);
                    }
                }
			} else {
				mToast.setGravity(Gravity.BOTTOM, 0, ConvertDipPx.dip2px(activity, 60));
                if(mToast.getView() instanceof LinearLayout){
                    if(((LinearLayout)mToast.getView()).getChildAt(0) instanceof ImageView){
                        ((LinearLayout)mToast.getView()).removeViewAt(0);
                    }
                }
			}
			mToast.show();
		}
	}
	
}
